package com.jessechanrui.template.common.template.auth.controller;

import com.jessechanrui.template.common.template.auth.application.dto.UserDTO;
import com.jessechanrui.template.common.template.auth.application.service.LoginService;
import com.jessechanrui.template.common.template.auth.common.AuthorizationSupport;
import com.jessechanrui.template.common.template.auth.common.Ignore;
import com.jessechanrui.template.core.dto.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenrui
 * @version 1.0
 * @description LoginController
 * @date 2025/4/30
 */
@Tag(name = "用户登录接口")
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Ignore
    @Operation(summary = "用户登录")
    @PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<UserDTO> login(@Parameter(description = "用户名", required = true) @RequestParam String userName,
                                         @Parameter(description = "用户密码", required = true) @RequestParam String password,
                                         @Parameter(description = "租户ID") @RequestParam(required = false) String tenantId) {
        return ResponseResult.of(loginService.login(userName, password, tenantId));
    }

    @Operation(summary = "退出登录")
    @PostMapping(value = "/logout", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult logout() {
        loginService.logout();
        return ResponseResult.buildSuccess();
    }

    @Operation(summary = "当前登录用户信息")
    @GetMapping(value = "/current", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<UserDTO> current() {
        return ResponseResult.of(AuthorizationSupport.getCurrentUser());
    }
}
